package com.sen.ssp.exception;

import java.util.List;

/**
 * 异常工具类
 * @author sen
 */
public final class ExceptionUtil {

	public ExceptionUtil() {
		// empty!
	}

	/**
	 * 提取异常中嵌套的简短消息（过滤了CallStack的信息）
	 */
	public static String getNestedMessage(List<Throwable> exceptions) {
		StringBuffer sb = new StringBuffer();

		if (exceptions == null) {
			return sb.toString();
		}
		for (int index = 0, len = exceptions.size(); index < len; index++) {
			Throwable t = exceptions.get(index);

			sb.append(String.format(" 异常序列 [%d] { ", index + 1));
			sb.append(getNestedMessage(t));
			sb.append(" } ");
		}

		return sb.toString();
	}

	public static String getNestedMessage(Throwable t) {
		StringBuffer sb = new StringBuffer();

		if (t == null) {
			return sb.toString();
		}
		sb.append(getNestedMessage(t));
		return sb.toString();
	}

}
